$(function () {
    //获取当前网址，如： http://localhost:8083/myproj/view/my.jsp
    var curWwwPath=window.document.location.href;
    //获取主机地址之后的目录，如： myproj/view/my.jsp
    var pathName=window.document.location.pathname;
    var pos=curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8083
    var localhostPaht=curWwwPath.substring(0,pos);
    //获取带"/"的项目名，如：/myproj
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    //得到了 http://localhost:8083/myproj
    var realPath=localhostPaht+projectName;
    $.ajax({
        url: realPath+"/banner/list",
        data: "",
        dataType: "json",
        type: 'POST',
        success: function(data) {
                var list = data.data;
                var str="";
                var indexs="";
                for(var i=0;i<list.length;i++){
                    str += "<img name='stalesname' width='900px' height='350px' src='http://192.168.0.20:8083/img/"+list[i].url+"' alt='"+i+"'>";
                    indexs += "<span data-index='"+i+"'></span>"
                }
                $("#list").html(str);
                $("#buttons").html(indexs)
        }
    });


})

let p=document.getElementsByClassName('img-g')[0];
let button=document.querySelectorAll('.button-g span')
window.timer=setInterval(move,3000);
function move(){
    if(parseInt(p.style.left)>-1200){
        p.style.left=parseInt(p.style.left)-700+'px'
        p.style.transition='left 1s';
        tog(-Math.round(parseInt(p.style.left)/700))
        if(parseInt(p.style.left)<=-1200){
            setTimeout(function(){
                tog(0)
                p.style.left='0px'
                p.style.transition='left 0s';
            },1000)
        }
    }else{
        p.style.left='0px'
        p.style.transition='left 0s';
    }
}

for(let i=0;i<button.length;i++){
    button[i].style.backgroundColor='#eee';
    button[i].onclick=function(){
        p.style.left=-700*this.getAttribute('data-index')+'px'
        tog(this.getAttribute('data-index'))
        clearInterval(window.timer)
        window.timer=setInterval(move,2000);
    }
}

function tog(index){
    if(index>1){tog(0);return;}
    for(let i=0;i<button.length;i++){
        button[i].style.backgroundColor='#eee'
    }
    button[index].style.backgroundColor='rgb(215, 81, 15)';
}
p.onmouseover=function(){
    clearInterval(window.timer)
}
p.onmouseout=function(){
    window.timer=setInterval(move,3000);
}

